﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using DBAccess;

namespace Task {
    internal class Tasks : ITasks
    {
        public Tasks()
        {}

        public DataTable GetTaskById(string id_task)
        {
            string sql = String.Format("SELECT id_task, name, description, id_project, status, cast (startdate as char) as startdate,  cast (enddate as char) as enddate  FROM Task WHERE id_task = '{0}'", id_task);
            DataTable dt = new DataTable();
            dt = BuildIExecute.Build().ExecuteQuery(sql);

            dt.Columns["id_task"].ColumnName = "#";
            dt.Columns["name"].ColumnName = "Наименование";
            dt.Columns["description"].ColumnName = "Описание";
            dt.Columns["id_project"].ColumnName = "Идентификатор проекта";
            dt.Columns["status"].ColumnName = "Статус";
            dt.Columns["startdate"].ColumnName = "Дата начала";
            dt.Columns["enddate"].ColumnName = "Дата окончания";

            return dt;
        }

        public DataTable GetAllTasks()
        {
            string sql = "SELECT id_task, name, description, id_project, status, cast (startdate as char) as startdate,  cast (enddate as char) as enddate  FROM Task";
            DataTable dt = new DataTable();
            dt = BuildIExecute.Build().ExecuteQuery(sql);

            dt.Columns["id_task"].ColumnName = "#";
            dt.Columns["name"].ColumnName = "Наименование";
            dt.Columns["description"].ColumnName = "Описание";
            dt.Columns["id_project"].ColumnName = "Идентификатор проекта";
            dt.Columns["status"].ColumnName = "Статус";
            dt.Columns["startdate"].ColumnName = "Дата начала";
            dt.Columns["enddate"].ColumnName = "Дата окончания";

            return dt;
        }

        public void AddTask(string name, string description, int id_project, int status, string start_date, string end_date)
        {
            Task t = new Task(name, description, id_project, start_date, end_date, status);
            string sql = String.Format("INSERT INTO Task (name, description, startdate, enddate, id_project, status) VALUES ('{0}' , '{1}', CAST ('{2}' as date) , CAST  ('{3}' as date), CAST ('{4}' as int), '{5}')", t.Name, t.Description, t.StartDate, t.EndDate, t.Id_project, t.Status);
            BuildIExecute.Build().ExecuteNonQuery(sql);
        }

        public void EditTask(string id_task)
        { }

        public void DeleteTask(string id_task)
        {
            int id_t = Convert.ToInt32(id_task);
            string sql = String.Format("DELETE FROM Task WHERE id_task = '{0}'", id_t);
            BuildIExecute.Build().ExecuteNonQuery(sql);
        }
    }
}
